<!-- 抄送我的-->
<template>
  <van-list
    v-model:loading="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="loadMore"
  >
    <van-cell-group v-for="item in list" :key="item">
      <van-cell title-style="min-width:70%;" :border="false">
        <template #title>
          <div class="card-title van-multi-ellipsis--l2">{{
            item.staterUser?.name + '提交的' + item.processDefName
          }}</div>
        </template>
        <template #value>
          <div class="fs-sm">{{ timeFormat(item.startTime, 'yyyy.mm.dd') }}</div>
        </template>
      </van-cell>
      <van-cell class="sub-title">
        <template #title>
          <div>开始时间: {{ timeFormat(item.startTime, 'yyyy-mm-dd hh:MM') }}</div>
          <div>结束时间: {{ timeFormat(item.finishTime, 'yyyy-mm-dd hh:MM') }}</div>
        </template>
        <template #label>
          <div class="status">{{ item.status }}</div>
        </template>
      </van-cell>
    </van-cell-group>
  </van-list>
</template>

<script setup lang="ts">
  import { ref, watch } from 'vue';
  import { Toast } from '@/utils/uniapi/prompt';
  import { onPullDownRefresh } from '@dcloudio/uni-app';
  import { timeFormat } from '@/utils/index';
  import { ResultEnum } from '@/enums/httpEnum';
  import { ApproveTypeEnum } from '../../enums/approveEnum';
  import { getCcMeList } from '../../services/api/approveTask';
  import { SubmitModel } from '../../services/model/approveTask/submitModel';
  const props = defineProps({
    type: {
      type: String,
      default: ApproveTypeEnum.UNDO,
    },
  });

  //监听页签变化，变化了就重新获取数据
  watch(
    () => props.type,
    (newValue) => {
      if (newValue !== loadType.value) {
        loadType.value = newValue;
      }
    },
  );
  //请求参数
  const params = ref({
    pageSize: 10,
    pageNo: 1,
    finished: null,
    code: '',
    procDefName: '',
  });
  const list = ref<SubmitModel[]>([]);
  const loading = ref(false);
  const finished = ref(false);
  const loadType = ref(props.type); //默认加载待我处理

  // 下拉刷新
  onPullDownRefresh(() => {
    //判断只刷新当前页签的数据
    if (loadType.value === ApproveTypeEnum.CC) {
      onRefresh();
    }
  });
  //下拉刷新
  const onRefresh = () => {
    list.value = []; // 清空列表数据
    finished.value = false;
    params.value.pageNo = 1; //默认加载第一页
    getList(); //加载数据
  };
  //加载更多
  const loadMore = () => {
    if (list.value.length == 0) {
      params.value.pageNo = 1;
    } else {
      params.value.pageNo++;
    }
    loading.value = false;
    getList();
  };
  // 加载数据
  const getList = () => {
    loading.value = true; // 将 loading 设置为 true，表示处于加载状态
    //我发起的
    getCcMeList(params.value).then(({ data }) => {
      if (data.code == ResultEnum.SUCCESS) {
        loading.value = false;
        if (params.value.pageNo == 1) {
          list.value = data.data.records;
        } else {
          data.data.records.forEach((t) => list.value.push(t));
        }
        // 判断当前页是否为最后一页
        if (params.value.pageNo * params.value.pageSize > data.data.total) {
          finished.value = true;
        }
      } else {
        finished.value = true;
        Toast(data.msg);
      }
      uni.stopPullDownRefresh();
    });
  };
</script>

<style scoped lang="scss">
  .van-cell-group {
    padding: 20rpx;
  }
  .van-cell {
    padding: 6rpx 8rpx;
  }
  .card-title {
    color: $uni-text-color;
    font-weight: 600;
  }
  .sub-title {
    color: #787878;
    font-size: 28rpx;
  }
  .status {
    color: #56b77a;
    font-weight: 600;
  }
</style>
